"use client"; import { useParams, useRouter } from "next/navigation"; import { useEffect } from "react"; import { useWorkspace } from "@/hooks/use-workspace"; import { useUiStore } from "@/stores/ui-store"; /** * [workspace] segment: resolves the workspace (by id or slug) from the URL. * Components under this route read the same workspace via useWorkspace(slug) * — one query key, no duplicate fetches. */ export default function WorkspaceLayout({ children }: { children: React.ReactNode }) { const params = useParams<{ workspace: string }>(); const slugOrId = params?.workspace ?? ""; const { isError } = useWorkspace(slugOrId); const router = useRouter(); const setSearchWorkspace = useUiStore((s) => s.setSearchWorkspace); // Unknown workspace → back to the default one. useEffect(() => { if (isError && slugOrId) { router.replace("/w/default"); } }, [isError, slugOrId, router]); // Keep the search dialog's workspace slug in sync with the route param so // the dialog (mounted at the (app) group, outside this route) scopes its // queries to the right workspace (TASK-0098). useEffect(() => { if (slugOrId) setSearchWorkspace(slugOrId); }, [slugOrId, setSearchWorkspace]); if (isError) return null; return <>{children}; } /** Current workspace slug from the route — for components under (app). */ export function useActiveWorkspaceSlug(): string { const params = useParams<{ workspace: string }>(); return params?.workspace ?? "default"; }